home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Embed / test.c < prev    next >
C/C++ Source or Header  |  1999-04-14  |  2KB  |  87 lines

  1. /* Example of embedding Python in another program */
  2.  
  3. #include "Python.h"
  4.  
  5. static char *argv0;
  6.  
  7. static const char *ver = "$VER: embed demo " __AMIGADATE__ " Embedded Python\0";
  8.  
  9.  
  10. void initxyzzy(Py_PROTO(void)); /* Forward */
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.     FILE *fh;
  15.  
  16.     /* Save a copy of argv0 */
  17.     argv0 = argv[0];
  18.  
  19.     /* Set the program name. Default is 'python' - but we are not called that way! */
  20.     Py_SetProgramName("test");
  21.  
  22.     /* Initialize the Python interpreter.  Required. */
  23.     Py_Initialize();
  24.  
  25.     /* Add a static module */
  26.     initxyzzy();
  27.  
  28.     /* Define sys.argv.  It is up to the application if you
  29.        want this; you can also let it undefined (since the Python 
  30.        code is generally not a main program it has no business
  31.        touching sys.argv...) */
  32.     PySys_SetArgv(argc, argv);
  33.  
  34.     /* Do some application specific code */
  35.     printf("Hello, brave new world\n\n");
  36.  
  37.     /* Execute some Python statements (in module __main__) */
  38.     PyRun_SimpleString("import sys\n");
  39.     PyRun_SimpleString("print 'BUILTIN MODULES:',sys.builtin_module_names\n");
  40.     PyRun_SimpleString("print 'IMPORTED MODULES:',sys.modules.keys()\n");
  41.     PyRun_SimpleString("print 'sys.argv:',sys.argv\n");
  42.  
  43.     fh = fopen("testscript.py","r");
  44.     if(fh)
  45.     {
  46.         PyRun_SimpleFile(fh,"testscript.py");
  47.         PyRun_SimpleString("print '>> testscript has set result to',result\n");
  48.         fclose(fh);
  49.     }
  50.  
  51.     /* Note that you can call any public function of the Python
  52.        interpreter here, e.g. call_object(). */
  53.  
  54.     /* Some more application specific code */
  55.     printf("\nGoodbye, cruel world\n");
  56.  
  57.     /* Exit, cleaning up the interpreter */
  58.     Py_Exit(0);
  59.     /*NOTREACHED*/
  60. }
  61.  
  62. /* A static module */
  63.  
  64. static PyObject *xyzzy_foo Py_PROTO((PyObject *, PyObject *));
  65.  
  66. static PyObject *
  67. xyzzy_foo(self, args)
  68.     PyObject *self; /* Not used */
  69.     PyObject *args;
  70. {
  71.     if (!PyArg_ParseTuple(args, ""))
  72.         return NULL;
  73.     return PyInt_FromLong(42L);
  74. }
  75.  
  76. static PyMethodDef xyzzy_methods[] = {
  77.     {"foo",        xyzzy_foo,    1},
  78.     {NULL,        NULL}        /* sentinel */
  79. };
  80.  
  81. void
  82. initxyzzy Py_PROTO((void))
  83. {
  84.     PyImport_AddModule("xyzzy");
  85.     Py_InitModule("xyzzy", xyzzy_methods);
  86. }
  87.